package com.jade.core.web.handler;

import cn.hutool.http.HttpStatus;
import com.jade.core.common.constant.ScanPkgConstant;
import com.jade.core.common.entity.BaseResult;
import com.jade.core.common.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 *  通用异常处理器
 *
 * @author Arnold 409196007@qq.com
 * @version V2.0
 * @date 2022/05/13 19:08
 **/
@Slf4j
@RestControllerAdvice(basePackages = {ScanPkgConstant.BASE_PACKAGE})
public class BaseExceptionHandler {
    /**
      * 自定义异常捕获处理
      * @param baseException 自定义异常
      * @return com.jade.core.common.entity.BaseResult
      * @author Arnold
      * @date 2022/7/7
      * @throws
      *
     **/
    @ExceptionHandler(BaseException.class)
    public BaseResult handleBaseException(BaseException baseException) {
        log.error("程序异常：{}", baseException.toString());
        return new BaseResult(String.valueOf(HttpStatus.HTTP_INTERNAL_ERROR), baseException.getMessage());
    }

    /**
      * 通用Exception异常捕获
      * @param exception 通用异常
      * @return com.jade.core.common.entity.BaseResult
      * @author Arnold
      * @date 2022/7/7
      * @throws
      *
     **/
    @ExceptionHandler(Exception.class)
    public BaseResult handleException(Exception exception) {
        log.error("程序异常：{}" , exception.toString());
        return new BaseResult(String.valueOf(HttpStatus.HTTP_INTERNAL_ERROR), exception.getMessage());
    }
}